const getApps = getApp().globalData
const util = require('../../utils/util.js')
const rate = wx.getSystemInfoSync().windowWidth / 750
const Monitor = require('../../utils/monitor.js').Monitor
// ********************************************************************************
Page(Monitor.hookApp({
  data: {
    swiperItem: [0, 1, 2, 3, 4],
    list: [
      [],
      [],
      [],
      [],
      []
    ],
    rate,
    loading: false,
    pageNum: 1,
    showNum: 4,
    length: 4,
    index: 0,
    tarbar: [{
      status: 99,
      image: getApps.imgUrl + "2e833201807261550333010.png",
      name: "全部",
    },
    {
      status: 0,
      image: getApps.imgUrl + "2e833201807261550333010.png",
      name: "待付款"
    },
    {
      status: 1,
      image: getApps.imgUrl + "2e833201807261550333010.png",
      name: "待发货"
    },
    {
      status: 3,
      image: getApps.imgUrl + "2e833201807261550333010.png",
      name: "待收货"
    },
    {
      status: 2,
      image: getApps.imgUrl + "2e833201807261550333010.png",
      name: "已取消"
    },
    ],
    imgUrls: getApp().globalData.imgUrls
  },
  jump(e) {
    if (e.currentTarget.dataset.class == 'pay') {
      wx.navigateTo({
        url: `../Dingdan/Dingdan?order_id=${e.currentTarget.dataset.orderid}`,
      })
    }
  },
  onLoad: function (options) {
    getApp().getPagesurl('my')
    this.setData({
      status: options.status
    })
  },
  onShow: function () {
    this.data.pageNum = 1
    this.data.list = []
    this.left()
  },
  bindscrolltolower() {
    this.data.pageNum++
    this.orderList('Noempty')
  },
  tarbarClick(e) {
    var status = e.currentTarget.dataset.item.status
    if (this.data.index != e.currentTarget.dataset.index) {
      this.setData({
        list: [],
        pageNum: 1,
        status,
        index: e.currentTarget.dataset.index
      })
    }

  },
  left() {
    var that = this
    this.data.tarbar.some((item, index) => {
      if (item.status == this.data.status) {
        that.setData({
          index
        })
        that.orderList()
      }
    })
  },
  bindchangeS(e) {
    var index = e.detail.current
    this.setData({
      list: [],
      pageNum: 1,
      index,
    })
    this.data.status = this.data.tarbar[index].status
    this.orderList()
  },
  orderList(Noempty) {
    var that = this
    util.API('pyapi/distribute_order_list', {
      status: that.data.status,
      page: that.data.pageNum,
      pageshow: 4,
    }, 'application/x-www-form-urlencoded').then(res => {

      let {
        code,
        orders_list,
        msg
      } = res
      if (code == 200) {
        orders_list.map((item, index) => {
          item.status = item.order__status || item.status;
          let totleNum = 0
          item.goods_list.map((items) => {
            totleNum += items.nums
          })
          item.totleNum = totleNum
        })

        if (Noempty) {} else {
          that.data.list = []
        }
        that.data.list = that.data.list.concat(orders_list);
        this.setData({
          list: that.data.list
        })
      } else {
        that.setData({
          loading: true,
        })
      }
    })
  },
  getLogistics(e) { //点击查看物流
    console.log(e)
    wx.navigateTo({
      url: `../Logistics/Logistics?order_id=${e.currentTarget.dataset.id}`,
    })
  },
}))